<?php
/**
 *
 * BEGIN BallTrajectoryDataHandler.php
 *
 *
 * @file BallTrajectoryDataHandler.php
 * @brief This class provides data access methods to generate game statistics
 * @author Thato Selebogo, Wits University
 * @copyright © Wits University 2011 - All Rights Reserved
 * @license BSD License (http://www.linfo.org/bsdlicense.html)
 * @date 21 October 2011
 *
 *
 * BEGIN CHANGELOG
 * Developers working on thee files are REQUIRED to maintain this CHANGELOG.
 * Please ensure you follow the Changelog Guidelines: http://code.google.com/p/dazzler/wiki/DazzlerDevelopGuideline?ts=1315476570&updated=DazzlerDevelopGuideline
 * !==================================================================================================================================================================
 *
 * VERSION 1.0.0
 * 	+ File Created, Thato Selebogo, 21 September 2011
 *
 *
 * ================================================================================================================================================================!
 * END CHANGELOG
 */
/* * *****************************************************************************************************************************************************************
 *
 * BEGIN Direct Call Check
 *
  /***************************************************************************************************************************************************************** */
class BallTrajectoryDataHandler extends baseClass {

    /**
     * Holds the database adapter singleton instance
     *
     * @var    DatabaseAdapterInterface
     * @access public
     */
    public $_databaseAdapter;

    protected $_databaseTable;

    /**
     * Constructor
     *
     * @param  void
     * @return void
     */
    public function __construct() {

        // Get database adapter and error logger from the base class
        $this->_databaseAdapter = parent::databaseAdapter();
        parent::parseLanguageErrorLogFile();
    }

        public function insertBallTrajectory($ballByBallId, $ballTrajectoryType, $ballTrajectoryValue) {
          // $ballTrajectoryValue = array("dfssdf"=>"sdfsdfsdf","sdfsdf"=>"sdfsdfdf");
           $ballTrajectoryValue = serialize($ballTrajectoryValue);
           $ballTrajectoryValue1 = base64_encode($ballTrajectoryValue);
            if($ballTrajectoryType != FALSE)
            {
           $ballTrajectoryData = array(
            'BALLTRAJECTORY_ID' => '',
            'BALLTRAJECTORY_TYPE' => $ballTrajectoryType,
            'BALLTRAJECTORY_VALUE' => $ballTrajectoryValue1,
            'BALLTRAJECTORY_BALLBYBALL_ID' => $ballByBallId)   ;
        $this->_databaseTable = 'BALLTRAJECTORY';
        $gameID = $this->_databaseAdapter->insert($this->_databaseTable, $ballTrajectoryData);
        return true;
        
        }

        }         /* !
     * \brief get ballTrajectory Data
     */

    public function getBallTrajectoryData($ballByBallId) {
        $query = "SELECT BALLTRAJECTORY_TYPE, BALLTRAJECTORY_VALUE
                  FROM BALLTRAJECTORY WHERE BALLTRAJECTORY_BALLBYBALL_ID = '$ballByBallId'";
        $this->_databaseAdapter->query($query);
        $data = $this->_databaseAdapter->fetch();
        $data['BALLTRAJECTORY_VALUE'] = unserialize(base64_decode($data['BALLTRAJECTORY_VALUE']));

        return $data;


    }

     public function getLastBallId($gameId)
    {
        $query = "SELECT BALLBYBALL_ID FROM BALLBYBALL WHERE BATSMAN_PLAYER_TEAM_GAME_GAME_ID = '$gameId'
                  ORDER BY BALLBYBALL_ID DESC LIMIT 1";
        $this->_databaseAdapter->query($query);
        $lastBallId = $this->_databaseAdapter->fetch();
        return $lastBallId['BALLBYBALL_ID'];
    }

     public function getLastBallTrajectory($gameId)
    {
        $lastBallId = $this->getLastBallId($gameId);

        $lastBallTrajectory = $this->getBallTrajectoryData($lastBallId);
        return $lastBallTrajectory;

    }

    public function getLastBallTrajectoryId($gameId)
    {

        $lastBallId = $this->getLastBallId($gameId);

        $query = "SELECT BALLTRAJECTORY_ID
                  FROM BALLTRAJECTORY WHERE BALLTRAJECTORY_BALLBYBALL_ID = '$lastBallId'";
        $this->_databaseAdapter->query($query);
        $data = $this->_databaseAdapter->fetch();

        return $data['BALLTRAJECTORY_ID'];

    }



}

?>
